package com.deer.wms.base.system.service.impl;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.deer.wms.base.system.dao.*;
import com.deer.wms.base.system.model.*;
import com.deer.wms.base.system.model.Item.*;
import com.deer.wms.base.system.model.Item.AuxProp;
import com.deer.wms.base.system.model.Supplier.SuppInfoBO;
import com.deer.wms.base.system.model.Supplier.SupplierInfo;
import com.deer.wms.base.system.service.ErpService;
import com.deer.wms.project.root.service.SuperServiceImpl;
import com.deer.wms.project.root.util.HttpClient;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Transactional
public class ErpServiceImpl extends SuperServiceImpl<ErpBackRecordMapper, ErpBackRecord> implements ErpService {

//    @Value("${lzb.erp.ip}")
//    private String erpIp;

    @Autowired
    private BillInfoMapper billInfoMapper;

    @Autowired
    private BillDetailMapper billDetailMapper;

    @Autowired
    private ItemInfoMapper itemInfoMapper;

    @Autowired
    private WarehouseMapper warehouseMapper;

    @Autowired
    private DeptInfoMapper deptInfoMapper;

    @Autowired
    private CustInfoMapper custInfoMapper;

    @Autowired
    private SuppInfoMapper suppInfoMapper;

    @Autowired
    private PurOrderMapper purOrderMapper;

    @Autowired
    private PurOrderDetailMapper purOrderDetailMapper;

    @Autowired
    private ItemInfoNewMapper itemInfoNewMapper;

    @Autowired
    private AuxPropMapper auxPropMapper;


    @Override
    public String saveBills() {
        try {
            //请求token
            MultiValueMap<String, String> stringMultiValueMap = new LinkedMultiValueMap<>();
            stringMultiValueMap.add("client_id", "KingdeeK3_Rggytest_EpichustClient");
            stringMultiValueMap.add("client_secret", "TekE%dAISMCvwk7V");
            stringMultiValueMap.add("grant_type", "client_credentials");
            String tokenUrl = "https://identityserver:44310/connect/token";
            String clientRg = HttpClient.clientRg(tokenUrl, HttpMethod.POST, stringMultiValueMap);
            JSONObject jsonObject = JSONObject.parseObject(clientRg);
            // 获取到key为shoppingCartItemList的值
            String token = jsonObject.getString("access_token");
            if(token != null){
                //调用erp接口获取数据
                HttpHeaders httpHeaders = new HttpHeaders();
                httpHeaders.add("Accept", "application/json");
//        httpHeaders.add("Authorization", propertyUtils.getDeviceTreeToken());
                httpHeaders.add("Authorization", "Bearer " + token);
                String url = "http://10.18.3.10:30001/api/dept/getalldepts";
                String result = HttpClient.getRequest(url, null, httpHeaders);
                JSONArray jsonArray = JSONArray.parseArray(result);
                List<BillBo> billBos = JSONObject.parseArray(jsonArray.toJSONString(), BillBo.class);
                if(billBos.size() != 0){
                    for(BillBo billBo : billBos){
                        //先判断抓取的数据在数据库中是否存在
                        QueryWrapper<BillInfo> qw = Wrappers.query();
                        Map<String, Object> map = new HashMap<>();
                        map.put("bill_no", billBo.getBillNo());
                        map.put("bill_type_id", billBo.getBillTypeId());
                        BillInfo info = billInfoMapper.selectOne(qw.allEq(map, false));
                        if(info == null){
                            BillInfo billInfo = new BillInfo();
                            billInfo.setBillTypeId(billBo.getBillTypeId());
                            //季工说字段会做调整，目前取值不定
                            billInfoMapper.insert(billInfo);
                            List<BillDetailBo> billDetailBos = billBo.getDetails();
                            for(BillDetailBo billDetailBo : billDetailBos){
                                BillDetail billDetail = new BillDetail();
                                billDetail.setItemId(billDetailBo.getItemId());
                                billDetail.setBillInfoId(billInfo.getId());
                                billDetailMapper.insert(billDetail);
                            }
                        }
                    }

                }

            }

            return null;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    @Override
    public String saveItemInfo() {
        try {
            //请求token
            MultiValueMap<String, String> stringMultiValueMap = new LinkedMultiValueMap<>();
            stringMultiValueMap.add("client_id", "KingdeeK3_Rggytest_EpichustClient");
            stringMultiValueMap.add("client_secret", "TekE%dAISMCvwk7V");
            stringMultiValueMap.add("grant_type", "client_credentials");
            String tokenUrl = "https://identityserver:44310/connect/token";
            String clientRg = HttpClient.clientRg(tokenUrl, HttpMethod.POST, stringMultiValueMap);
            JSONObject jsonObject = JSONObject.parseObject(clientRg);
            String token = jsonObject.getString("access_token");
            System.out.println("token ==》" + token);
            if(token != null){

                for(int i = 1 ; i<=27; i++){
                    Map<String, Integer> map1 = new HashMap<>();
                    map1.put("currentPage",i);
                    map1.put("pageSize",10000);

                    //调用erp接口获取数据
                    CloseableHttpClient client = HttpClients.createDefault();
                    URIBuilder uriBuilder = new URIBuilder("http://10.18.3.10:30001/api/ICItem/GetRangeICItems");
                    HttpPost post = new HttpPost(uriBuilder.build());
                    StringEntity myEntity = new StringEntity(JSON.toJSONString(map1), "UTF-8");// 构造请求数据
                    post.setHeader("Content-Type", "application/json");
                    post.addHeader("Authorization", "Bearer " + token);
                    post.setEntity(myEntity);// 设置请求体
                    String responseContent = null; // 响应内容
                    CloseableHttpResponse response = null;
                    try {
                        response = client.execute(post);
                        HttpEntity entity = response.getEntity();
                        if (response.getStatusLine().getStatusCode() == 200) {
                            String content = EntityUtils.toString(entity, "UTF-8");
                            ItemResponseBO itemResponseBO = JSONArray.parseObject(content, ItemResponseBO.class);
                            String it = itemResponseBO.getList();
                            List<ItemErpBO> itemErpBOS = (List<ItemErpBO>) JSON
                                    .parseArray(it, ItemErpBO.class);
//                    System.out.println("11111111111111111");
                            if(itemErpBOS.size() != 0){
                                for(ItemErpBO bo : itemErpBOS){
                                    //先判断抓取的数据在数据库中是否存在
//                                    QueryWrapper<ItemInfo> qw = Wrappers.query();
//                                    ItemInfo info = itemInfoMapper.selectOne(qw.eq("item_code", bo.getNumber()));
//                                    if(info == null){
//                                        ItemInfo itemInfo = new ItemInfo();
//                                        itemInfo.setUpperLimit(bo.getId());//主键
//                                        itemInfo.setItemCode(bo.getNumber());//物料编码
//                                        itemInfo.setItemName(bo.getName());//物料名称
//                                        itemInfo.setItemStatus(bo.getEnabled() == false ? "disable" : "enable");//启用
//                                        itemInfo.setSpec(bo.getModel());//规格
//                                        itemInfo.setFloorLimit(bo.getUnitId());//计量单位ID
//                                        itemInfo.setUnitName(bo.getUnitName());//主计量单位名称
//                                        itemInfo.setOrganizationId(bo.getErpClsId());//物料ID
//                                        itemInfo.setItemBarCode(bo.getErpClsName());//物料属性
//                                        itemInfo.setItemTypeId(bo.getAuxPropClassId());//物料辅助属性类别ID
//                                        //批次管理
//                                        itemInfo.setMemo(bo.getBatchManager() == false ||bo.getBatchManager() == null ? "0" : "1");
//                                        itemInfo.setWeight(bo.getQuality());//材质
//                                        itemInfo.setSupplierId(bo.getPoInspectionMethodId());//采购检验ID
//                                        itemInfo.setModel(bo.getPoInspectionMethodName());//采购检验
//                                        itemInfo.setPackCode(bo.getShelfLifeManager() == false ||bo.getShelfLifeManager() == null ? "0" : "1");
//                                        itemInfo.setBatchTacticCode(bo.getShelfLifeDays().toString());
//                                        itemInfoMapper.insert(itemInfo);
//                                        System.out.println("保存了物料信息");
//                                    }

                                    QueryWrapper<ItemInfoNew> qw = Wrappers.query();
//                                    ItemInfoNew info = itemInfoNewMapper.selectOne(qw.eq("item_number", bo.getNumber()));
//                                    if(info == null){
                                        ItemInfoNew itemInfoNew = new ItemInfoNew();
                                        itemInfoNew.setItemId(bo.getId());
                                        itemInfoNew.setItemNumber(bo.getNumber());
                                        itemInfoNew.setItemName(bo.getName());
                                        itemInfoNew.setUsed(bo.getEnabled());
                                        itemInfoNew.setModel(bo.getModel());
                                        itemInfoNew.setUnitId(bo.getUnitId());
                                        itemInfoNew.setUnitName(bo.getUnitName());
                                        itemInfoNew.setErpClsId(bo.getErpClsId());
                                        itemInfoNew.setErpClsName(bo.getErpClsName());
                                        itemInfoNew.setAuxPropClassId(bo.getAuxPropClassId());
                                        itemInfoNew.setBatchManager(bo.getBatchManager());
                                        itemInfoNew.setShelfLifeManager(bo.getShelfLifeManager());
                                        itemInfoNew.setShelfLifeDays(bo.getShelfLifeDays());
                                        itemInfoNew.setPoInspectionMethodId(bo.getPoInspectionMethodId());
                                        itemInfoNew.setPoInspectionMethodName(bo.getPoInspectionMethodName());
                                        itemInfoNew.setQuality(bo.getQuality());
                                        itemInfoNewMapper.insert(itemInfoNew);
//                                    }

                                }

                            }

                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }  finally {
                        try {
                            if (response != null)
                                response.close();

                        } catch (IOException e) {
                            e.printStackTrace();
                        } finally {
                            try {
                                if (client != null)
                                    client.close();
                            } catch (IOException e) {
                                e.printStackTrace();
                            }
                        }
                    }
                }


            }
            return null;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    @Override
    public String saveWarehouseInfo() {

        try {
            //请求token
            MultiValueMap<String, String> stringMultiValueMap = new LinkedMultiValueMap<>();
            stringMultiValueMap.add("client_id", "KingdeeK3_Rggytest_EpichustClient");
            stringMultiValueMap.add("client_secret", "TekE%dAISMCvwk7V");
            stringMultiValueMap.add("grant_type", "client_credentials");
            String tokenUrl = "https://identityserver:44310/connect/token";
            String clientRg = HttpClient.clientRg(tokenUrl, HttpMethod.POST, stringMultiValueMap);
            JSONObject jsonObject = JSONObject.parseObject(clientRg);
            String token = jsonObject.getString("access_token");
            if(token != null){
                //调用erp接口获取数据
                HttpHeaders httpHeaders = new HttpHeaders();
                httpHeaders.add("Accept", "application/json");
                httpHeaders.add("Authorization", "Bearer " + token);
                String url = "http://10.18.3.10:30001/api/Stock/GetAllStocks";
                String result = HttpClient.getRequest(url, null, httpHeaders);
                JSONArray jsonArray = JSONArray.parseArray(result);
                List<WarehouseBO> ws = JSONObject.parseArray(jsonArray.toJSONString(), WarehouseBO.class);
                if(ws.size() != 0){
                    for(WarehouseBO bo : ws){
                        //先判断抓取的数据在数据库中是否存在
                        QueryWrapper<Warehouse> qw = Wrappers.query();
                        Warehouse info = warehouseMapper.selectOne(qw.eq("war_num", bo.getNumber()));

                        if(info == null){
                            Warehouse wh = new Warehouse();
                            wh.setWarNum(bo.getNumber());
                            //季工说字段会做调整，目前取值不定
                            wh.setWarName(bo.getName());
                            wh.setWarId(bo.getId());
                            wh.setWarGroupId(bo.getSPGroupId());
                            wh.setStockPlaceId(bo.getStockPlaceId());
                            wh.setUsed(bo.getEnabled());
                            warehouseMapper.insert(wh);
                        }
                    }
                }
            }
            return null;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    @Override
    public String saveDeptInfo() {
        try {
            //请求token
            MultiValueMap<String, String> stringMultiValueMap = new LinkedMultiValueMap<>();
            stringMultiValueMap.add("client_id", "KingdeeK3_Rggytest_EpichustClient");
            stringMultiValueMap.add("client_secret", "TekE%dAISMCvwk7V");
            stringMultiValueMap.add("grant_type", "client_credentials");
            String tokenUrl = "https://identityserver:44310/connect/token";
            String clientRg = HttpClient.clientRg(tokenUrl, HttpMethod.POST, stringMultiValueMap);
            JSONObject jsonObject = JSONObject.parseObject(clientRg);
            String token = jsonObject.getString("access_token");
            if(token != null){
                //调用erp接口获取数据
                HttpHeaders httpHeaders = new HttpHeaders();
                httpHeaders.add("Accept", "application/json");
                httpHeaders.add("Authorization", "Bearer " + token);
                String url = "http://10.18.3.10:30001/api/dept/getalldepts";
                String result = HttpClient.getRequest(url, null, httpHeaders);
                JSONArray jsonArray = JSONArray.parseArray(result);
                List<DeptInfoBO> di = JSONObject.parseArray(jsonArray.toJSONString(), DeptInfoBO.class);
                if(di.size() != 0){
                    for(DeptInfoBO bo : di){
                        //先判断抓取的数据在数据库中是否存在
                        QueryWrapper<DeptInfo> qw = Wrappers.query();
                        DeptInfo info = deptInfoMapper.selectOne(qw.eq("dep_num", bo.getNumber()));

                        if(info == null){
                            DeptInfo fo = new DeptInfo();
                            fo.setDepNum(bo.getNumber());
                            //季工说字段会做调整，目前取值不定
                            fo.setDepName(bo.getName());
                            fo.setDepId(bo.getId());
                            fo.setIsAbled(bo.getEnabled());
                            deptInfoMapper.insert(fo);
                        }
                    }
                }
            }
            return null;

        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    @Override
    public String saveCustomerInfo() {

        try {
            //请求token
            MultiValueMap<String, String> stringMultiValueMap = new LinkedMultiValueMap<>();
            stringMultiValueMap.add("client_id", "KingdeeK3_Rggytest_EpichustClient");
            stringMultiValueMap.add("client_secret", "TekE%dAISMCvwk7V");
            stringMultiValueMap.add("grant_type", "client_credentials");
            String tokenUrl = "https://identityserver:44310/connect/token";
            String clientRg = HttpClient.clientRg(tokenUrl, HttpMethod.POST, stringMultiValueMap);
            JSONObject jsonObject = JSONObject.parseObject(clientRg);
            String token = jsonObject.getString("access_token");
            if(token != null){
                //调用erp接口获取数据
                HttpHeaders httpHeaders = new HttpHeaders();
                httpHeaders.add("Accept", "application/json");
                httpHeaders.add("Authorization", "Bearer " + token);
                String url = "http://10.18.3.10:30001/api/Customer/GetAllCustomers";
                String result = HttpClient.getRequest(url, null, httpHeaders);
                JSONArray jsonArray = JSONArray.parseArray(result);
                List<CustInfoBO> ci = JSONObject.parseArray(jsonArray.toJSONString(), CustInfoBO.class);
                if(ci.size() != 0){
                    for(CustInfoBO bo : ci){
                        //先判断抓取的数据在数据库中是否存在
                        QueryWrapper<CustomerInfo> qw = Wrappers.query();
                        CustomerInfo info = custInfoMapper.selectOne(qw.eq("cus_num", bo.getNumber()));

                        if(info == null){
                            CustomerInfo customerInfo = new CustomerInfo();
                            customerInfo.setCusNum(bo.getNumber());
                            customerInfo.setCusId(bo.getId());
                            customerInfo.setCusName(bo.getName());
                            customerInfo.setIsAbled(bo.getEnabled());
                            //季工说字段会做调整，目前取值不定
                            custInfoMapper.insert(customerInfo);
                        }
                    }
                }
            }

        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }

        return null;
    }

    @Override
    public String saveSupplierInfo() {

        try {
            //请求token
            MultiValueMap<String, String> stringMultiValueMap = new LinkedMultiValueMap<>();
            stringMultiValueMap.add("client_id", "KingdeeK3_Rggytest_EpichustClient");
            stringMultiValueMap.add("client_secret", "TekE%dAISMCvwk7V");
            stringMultiValueMap.add("grant_type", "client_credentials");
            String tokenUrl = "https://identityserver:44310/connect/token";
            String clientRg = HttpClient.clientRg(tokenUrl, HttpMethod.POST, stringMultiValueMap);
            JSONObject jsonObject = JSONObject.parseObject(clientRg);
            String token = jsonObject.getString("access_token");
            if(token != null){
                //调用erp接口获取数据
                HttpHeaders httpHeaders = new HttpHeaders();
                httpHeaders.add("Accept", "application/json");
                httpHeaders.add("Authorization", "Bearer " + token);
                String url = "http://10.18.3.10:30001/api/Supplier/GetAllSuppliers";
                String result = HttpClient.getRequest(url, null, httpHeaders);
                JSONArray jsonArray = JSONArray.parseArray(result);
                List<SuppInfoBO> si = JSONObject.parseArray(jsonArray.toJSONString(), SuppInfoBO.class);
                if(si.size() != 0){
                    for(SuppInfoBO bo : si){
                        //先判断抓取的数据在数据库中是否存在
                        QueryWrapper<SupplierInfo> qw = Wrappers.query();
                        SupplierInfo info = suppInfoMapper.selectOne(qw.eq("sup_num", bo.getNumber()));

                        if(info == null){
                            SupplierInfo supplierInfo = new SupplierInfo();
                            supplierInfo.setSupNum(bo.getNumber());
                            supplierInfo.setSupId(bo.getId());
                            supplierInfo.setSupName(bo.getName());
                            supplierInfo.setIsAbled(bo.getEnabled());
                            //季工说字段会做调整，目前取值不定
                            suppInfoMapper.insert(supplierInfo);
                        }
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }

        return null;
    }

    @Override
    public String savePurchaseOrder() {

        try {
            //请求token
            System.out.println("请求token");
            MultiValueMap<String, String> stringMultiValueMap = new LinkedMultiValueMap<>();
            stringMultiValueMap.add("client_id", "KingdeeK3_Rggytest_EpichustClient");
            stringMultiValueMap.add("client_secret", "TekE%dAISMCvwk7V");
            stringMultiValueMap.add("grant_type", "client_credentials");
            String tokenUrl = "https://identityserver:44310/connect/token";
            String clientRg = HttpClient.clientRg(tokenUrl, HttpMethod.POST, stringMultiValueMap);
            JSONObject jsonObject = JSONObject.parseObject(clientRg);
            String token = jsonObject.getString("access_token");
            System.out.println("token" + token);

            if(token != null){
                //调用erp接口获取数据
                HttpHeaders httpHeaders = new HttpHeaders();
                httpHeaders.add("Accept", "application/json");
                httpHeaders.add("Authorization", "Bearer " + token);
                String url = "http://10.18.3.10:30001/api/Epichust/GetAllIMInSourceBills";
                String result = HttpClient.getRequest(url, null, httpHeaders);
                System.out.println("result" + result);
                JSONArray jsonArray = JSONArray.parseArray(result);
                List<PurchaseOrderBO> po = JSONObject.parseArray(jsonArray.toJSONString(), PurchaseOrderBO.class);

                if(po.size() != 0){
                    for(PurchaseOrderBO bo : po){
                        //先判断抓取的数据在数据库中是否存在
                        QueryWrapper<PurchaseOrder> qw = Wrappers.query();
                        Map<String, Object> map = new HashMap<>();
                        map.put("bill_no", bo.getBillNo());
                        map.put("bill_type_id", bo.getBillTypeId());
                        PurchaseOrder info = purOrderMapper.selectOne(qw.allEq(map, false));

                        if(info == null){
                            PurchaseOrder purchaseOrder = new PurchaseOrder();
                            purchaseOrder.setOrderId(bo.getId());
                            purchaseOrder.setBillTypeId(bo.getBillTypeId());
                            purchaseOrder.setBillTypeName(bo.getBillTypeName());
                            purchaseOrder.setBillNo(bo.getBillNo());
//                            purchaseOrder.setBillDate(bo.getBillDate());
                            purchaseOrder.setBillStatus(bo.getBillStatus());
                            purchaseOrder.setBillClosed(bo.getBillClosed());
                            purchaseOrder.setSupplierId(bo.getSupplierId());
                            purchaseOrder.setSupplierName(bo.getSupplierName());
                            purchaseOrder.setCustomerId(bo.getCustomerId());
                            purchaseOrder.setCustomerName(bo.getCustomerName());
                            purchaseOrder.setDeptId(bo.getDeptId());
                            purchaseOrder.setDeptName(bo.getDeptName());
                            purchaseOrder.setRemark(bo.getRemark());
                            purOrderMapper.insert(purchaseOrder);
                            System.out.println("保存了主表信息");
                            List<PurchaseOrderDetailBO> purchaseOrderDetailBOS = bo.getDetails();
                            for(PurchaseOrderDetailBO purchaseOrderDetailBO :purchaseOrderDetailBOS){
                                PurOrderDetail purOrderDetail = new PurOrderDetail();
                                purOrderDetail.setPurId(purchaseOrder.getId());
                                purOrderDetail.setRowIndex(purchaseOrderDetailBO.getRowIndex());
                                purOrderDetail.setItemId(purchaseOrderDetailBO.getItemId());
                                purOrderDetail.setItemNumber(purchaseOrderDetailBO.getItemNumber());
                                purOrderDetail.setItemName(purchaseOrderDetailBO.getItemName());
                                purOrderDetail.setItemModel(purchaseOrderDetailBO.getItemModel());
                                purOrderDetail.setAuxPropClassId(purchaseOrderDetailBO.getAuxPropClassId());
                                purOrderDetail.setAuxPropId(purchaseOrderDetailBO.getAuxPropId());
                                purOrderDetail.setAuxPropName(purchaseOrderDetailBO.getAuxPropName());
//                                purOrderDetail.setAuxUnitId(purchaseOrderDetailBO.getAuxUnitId());
//                                purOrderDetail.setAuxUnitName(purchaseOrderDetailBO.getAuxUnitName());
                                purOrderDetail.setBatchManager(purchaseOrderDetailBO.getBatchManager());
                                purOrderDetail.setBatchNo(purchaseOrderDetailBO.getBatchNo());
//                                purOrderDetail.setAuxQty(purchaseOrderDetailBO.getAuxQty());
//                                purOrderDetail.setAuxCommitQty(purchaseOrderDetailBO.getAuxCommitQty());
                                purOrderDetail.setUnitId(purchaseOrderDetailBO.getUnitId());
                                purOrderDetail.setQty(purchaseOrderDetailBO.getQty());
//                                purOrderDetail.setCommitQty(purchaseOrderDetailBO.getCommitQty());
//                                purOrderDetail.setPlanModeId(purchaseOrderDetailBO.getPlanModeId());
                                purOrderDetail.setMtoNo(purchaseOrderDetailBO.getMtoNo());
                                purOrderDetail.setRemark(purchaseOrderDetailBO.getRemark());
                                purOrderDetail.setTargetBillTypeId(purchaseOrderDetailBO.getTargetBillTypeId());
                                purOrderDetail.setTargetBillRob(purchaseOrderDetailBO.getTargetBillROB());
                                purOrderDetailMapper.insert(purOrderDetail);
                                System.out.println("保存了子表信息");
                            }
                        }

                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }

        return null;
    }

    public static void main(String[] args) throws Exception {

        //请求token
        MultiValueMap<String, String> stringMultiValueMap = new LinkedMultiValueMap<>();
        stringMultiValueMap.add("client_id", "KingdeeK3_Rggytest_EpichustClient");
        stringMultiValueMap.add("client_secret", "TekE%dAISMCvwk7V");
        stringMultiValueMap.add("grant_type", "client_credentials");
        String tokenUrl = "https://identityserver:44310/connect/token";
        String clientRg = HttpClient.clientRg(tokenUrl, HttpMethod.POST, stringMultiValueMap);
        JSONObject jsonObject = JSONObject.parseObject(clientRg);
        String token = jsonObject.getString("access_token");
        System.out.println("token ==》" + token);

        if(token != null){

            Map<String, Integer> map1 = new HashMap<>();
            map1.put("currentPage",1);
            map1.put("pageSize",100);

            //调用erp接口获取数据
            CloseableHttpClient client = HttpClients.createDefault();
            URIBuilder uriBuilder = new URIBuilder("http://10.18.3.10:30001/api/ICItem/GetRangeICItems");
            HttpPost post = new HttpPost(uriBuilder.build());
            StringEntity myEntity = new StringEntity(JSON.toJSONString(map1), "UTF-8");// 构造请求数据
            post.setHeader("Content-Type", "application/json");
            post.addHeader("Authorization", "Bearer " + token);
            post.setEntity(myEntity);// 设置请求体
            String responseContent = null; // 响应内容
            CloseableHttpResponse response = null;
            try {
                response = client.execute(post);
                HttpEntity entity = response.getEntity();
                if (response.getStatusLine().getStatusCode() == 200) {
                    String content = EntityUtils.toString(entity, "UTF-8");
                    ItemResponseBO itemResponseBO = JSONArray.parseObject(content, ItemResponseBO.class);
                    String it = itemResponseBO.getList();
                    List<ItemErpBO> outList = (List<ItemErpBO>) JSON
                            .parseArray(it, ItemErpBO.class);
//                    System.out.println("11111111111111111");

                }
            } catch (Exception e) {
                e.printStackTrace();
            }  finally {
                try {
                    if (response != null)
                        response.close();

                } catch (IOException e) {
                    e.printStackTrace();
                } finally {
                    try {
                        if (client != null)
                            client.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    }

    @Override
    public String saveAuxProps() {

        try {
            //请求token
            MultiValueMap<String, String> stringMultiValueMap = new LinkedMultiValueMap<>();
            stringMultiValueMap.add("client_id", "KingdeeK3_Rggytest_EpichustClient");
            stringMultiValueMap.add("client_secret", "TekE%dAISMCvwk7V");
            stringMultiValueMap.add("grant_type", "client_credentials");
            String tokenUrl = "https://identityserver:44310/connect/token";
            String clientRg = HttpClient.clientRg(tokenUrl, HttpMethod.POST, stringMultiValueMap);
            JSONObject jsonObject = JSONObject.parseObject(clientRg);
            String token = jsonObject.getString("access_token");
            System.out.println("token ==》" + token);

            if(token != null){

//                for(int i = 1 ; i<=8; i++){
                    Map<String, Integer> map1 = new HashMap<>();
                    map1.put("currentPage",1);
                    map1.put("pageSize",10000);

                    //调用erp接口获取数据
                    CloseableHttpClient client = HttpClients.createDefault();
                    URIBuilder uriBuilder = new URIBuilder("http://10.18.3.10:30001/api/AuxProp/GetRangeAuxProps/3033");
                    HttpPost post = new HttpPost(uriBuilder.build());
                    StringEntity myEntity = new StringEntity(JSON.toJSONString(map1), "UTF-8");// 构造请求数据
                    post.setHeader("Content-Type", "application/json");
                    post.addHeader("Authorization", "Bearer " + token);
                    post.setEntity(myEntity);// 设置请求体
                    String responseContent = null; // 响应内容
                    CloseableHttpResponse response = null;
                    try {
                        response = client.execute(post);
                        HttpEntity entity = response.getEntity();
                        if (response.getStatusLine().getStatusCode() == 200) {
                            String content = EntityUtils.toString(entity, "UTF-8");
                            ItemResponseBO itemResponseBO = JSONArray.parseObject(content, ItemResponseBO.class);
                            String it = itemResponseBO.getList();
                            List<AuxPropBO> auxPropBOS = (List<AuxPropBO>) JSON
                                    .parseArray(it, AuxPropBO.class);
                            if(auxPropBOS.size() != 0){
                                for(AuxPropBO bo : auxPropBOS){
                                    AuxProp auxProp = new AuxProp();
                                    auxProp.setAuxId(bo.getId());
                                    auxProp.setAuxName(bo.getName());
                                    auxProp.setAuxNumber(bo.getNumber());
                                    auxProp.setUsed(bo.getEnabled());
                                    auxProp.setAuxTypeId(3033);
                                    auxPropMapper.insert(auxProp);

                                }

                            }

                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }  finally {
                        try {
                            if (response != null)
                                response.close();

                        } catch (IOException e) {
                            e.printStackTrace();
                        } finally {
                            try {
                                if (client != null)
                                    client.close();
                            } catch (IOException e) {
                                e.printStackTrace();
                            }
                        }
                    }
//                }


            }

            return null;
        }catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

}
